using System.Collections;
using UnityEngine;

public class Enemy100 : TypeOfEnemy
{
	private Coroutine co;

	private bool getDamage;

	private int meleeAttackCount;

	public void Init(int arrayNum, int weaponID)
	{
		arrayNumForAllObjectsInfo = arrayNum;
		SetEnemy();
		ChangeMotionOfEnemy();
		StopAllCoroutines();
		StartCoroutine(Motions());
		StartCoroutine(RefreshPosition());
		StartCoroutine(IE_Update());
		StartCoroutine(IE_DrawShadow());
		isVisibleOnScreen = false;
		grabingWeaponID = weaponID;
		EquipGetOrDrop("Enemy", true, 0, grabingWeaponID);
		ChangeCharacterAnimType(GetKindOfWeaponNumber());
		SR_Helm.sprite = Sprites.instance.helmSpriteList[7];
		SR_Helm.color = VariousInfo.cloakInfo[(PlayerPrefs.GetInt("currentUnlockedCloakNumber") != 0) ? PlayerPrefs.GetInt("currentUnlockedCloakNumber") : 10].c;
		characterGravity = 0f;
		rb.gravityScale = characterGravity;
		rb.velocity = Vector2.zero;
		SCRT_Cloak.ChangeColor(VariousInfo.cloakInfo[PlayerPrefs.GetInt("currentUnlockedCloakNumber") + 1].c);
		getDamage = false;
	}

	public void ChangeMotionOfEnemy()
	{
		animClipOverrides["StickMan_MotionOfEnemy0"] = AnimClips.instance.motionOfEnemy[1];
		atoc.ApplyOverrides(animClipOverrides);
	}

	private IEnumerator Motions()
	{
		yield return new WaitForFixedUpdate();
		BodyTrailSwtich(2f);
		yield return new WaitForFixedUpdate();
		hp = maxHP + maxHP * (float)HiddenLevelController.instance.hiddenLevel * 0.2f;
		currentOnTheFloorFlag = false;
		MoveToThePosition(new Vector2(Player.instance.tf.position.x + 12f, 1.5f), new Vector2(-99999f, -99999f));
		LookAtThePlayer();
		yield return new WaitForSeconds(0.5f);
		TextBubble.instance.Show(new Vector2(RB_IKList[0].position.x - 2f, RB_IKList[0].position.y + 7f), VariousInfo.hiddenBossSpeechList[Random.Range(0, VariousInfo.hiddenBossSpeechList.Length)], 1.5f);
		yield return new WaitForSeconds(1.8f);
		getDamage = true;
		while (Player.instance.hp > 0f)
		{
			yield return new WaitForSeconds(0.1f);
			if (CheckAction())
			{
				continue;
			}
			if (freezingTime > 0f)
			{
				freezingTime -= 0.1f;
				continue;
			}
			isVisibleOnScreen = Calc.IsVisibleCharacterOnScreen(tf.position);
			LookAtThePlayer();
			if (Random.Range(0, 5) == 0 || meleeAttackCount > 2)
			{
				MoveToThePosition(new Vector2(Random.Range(-20f, 20f) + Cam.instance.tf.position.x, Random.Range(15, 4)), new Vector2(-99999f, -99999f));
				LookAtThePlayer();
				ShowMotionOfEnemy();
				meleeAttackCount = 0;
			}
			else
			{
				currentOnTheFloorFlag = true;
				MoveToThePosition(Player.instance.tf.position, new Vector2(Player.instance.tf.position.x, Player.instance.tf.position.y + 2.5f));
				meleeAttackCount++;
			}
		}
		characterGravity = 3f;
		rb.gravityScale = characterGravity;
	}

	public void MoveToThePosition(Vector2 touchedPos, Vector2 targetPos)
	{
		rb.velocity = new Vector2(0f, 0f);
		if (Calc.KindOfWeapon(grabingWeaponID) != 6)
		{
			at.Play(dash_NormalList[0], -1, 0f);
		}
		BodyTrailSwtich(2f);
		LookAtTheTarget(touchedPos, targetPos);
		if (targetPos.x != -99999f)
		{
			StandUpTheEnemyFront(targetPos);
			Attack();
		}
		else
		{
			tf.position = new Vector2(touchedPos.x, touchedPos.y - 2f);
		}
	}

	public void StandUpTheEnemyFront(Vector2 targetCharacterPos)
	{
		float num = Vector2.Distance(targetCharacterPos * Player.instance.hitSize, tf.position);
		if (num < attackRange.x)
		{
			tf.position = new Vector2(tf.position.x, targetCharacterPos.y - 2.7f);
		}
		else if (tf.position.x > targetCharacterPos.x)
		{
			tf.position = new Vector2(targetCharacterPos.x + (Player.instance.hitSize + attackRange.x), targetCharacterPos.y - 2.7f);
		}
		else
		{
			tf.position = new Vector2(targetCharacterPos.x - (Player.instance.hitSize + attackRange.x), targetCharacterPos.y - 2.7f);
		}
	}

	public void LookAtTheTarget(Vector2 touchedPos, Vector2 targetPos)
	{
		if (tf.position.x > ((targetPos.x == -99999f) ? touchedPos.x : targetPos.x))
		{
			tf.localScale = new Vector3(characterScale, characterScale, 1f);
		}
		else if (tf.position.x < ((targetPos.x == -99999f) ? touchedPos.x : targetPos.x))
		{
			tf.localScale = new Vector3(0f - characterScale, characterScale, 1f);
		}
	}

	public void OtherJumpToTheTarget()
	{
		rb.velocity = ((!(tf.position.x < Player.instance.tf.position.x)) ? new Vector2(Random.Range(-3, -8), Random.Range(15, 22)) : new Vector2(Random.Range(3, 8), Random.Range(15, 22)));
	}

	public void ShowMotionOfEnemy()
	{
		attackingNow = true;
		kindOfMotion = 1;
		at.Play(motionOfEnemyList[0], -1, (float)HiddenLevelController.instance.hiddenLevel * 0.01f);
		Generator.instance.SpawnObj(30, RB_IKList[0].position);
		ShowAttackRange("CircleCol");
		SlowMotionControl(0.5f - (float)HiddenLevelController.instance.hiddenLevel * 0.01f);
	}

	public void ShootHommingMagic1()
	{
		Generator.instance.SpawnObj(28, RB_IKList[3].position, new float[1] { (0f - tf.localScale.x) * ((float)(HiddenLevelController.instance.hiddenLevel + 18) * 0.05f) }, Player.instance.tf);
	}

	public override void Damaged(Vector2 attackedPos, AttackInfo attackInfo)
	{
		if (hp <= 0f)
		{
			return;
		}
		Generator.instance.SpawnObj(9, new Vector2(TF_CenterBone.position.x, TF_CenterBone.position.y));
		hp -= (getDamage ? attackInfo.power : 0);
		AllObjectsInfo.enemy[arrayNumForAllObjectsInfo].hp = hp;
		Cam.instance.Shake(0.1f, 0.3f);
		if (attackInfo.power > 0)
		{
			GM.instance.IMG_EmenyHP.fillAmount = Calc.Percentage(hp, maxHP + maxHP * (float)HiddenLevelController.instance.hiddenLevel * 0.2f);
		}
		Audio.instance.PlayWeaponSFX(attackInfo.kindOfWeapon);
		ComboText.instance.AddCombo();
		GM.instance.SlowMotion(0.02f, (float)attackInfo.power * 0.0002f + 0.1f);
		if (hp <= 0f)
		{
			characterGravity = 3f;
			rb.gravityScale = characterGravity;
			StartCoroutine(IE_Die(attackInfo.force));
			return;
		}
		if (co != null)
		{
			StopCoroutine(co);
		}
		co = StartCoroutine(DamagedAndMove(Random.Range(0.2f, 0.5f)));
	}

	private IEnumerator DamagedAndMove(float time)
	{
		yield return new WaitForSeconds(0f);
		if (hp > 0f)
		{
		}
		co = null;
	}

	private IEnumerator IE_Die(Vector2 forceVel)
	{
		Cam.instance.ZoomInAndOutForCharacterDie(RB_IKList[0].transform);
		COL_AttackZone.gameObject.SetActive(false);
		COL_HitBox.enabled = false;
		attackingNow = false;
		GO_Blood.SetActive(true);
		GO_Blood.transform.localScale = tf.localScale;
		yield return new WaitForSeconds(0.02f);
		at.Play("StickMan_Damaged", -1, 1f);
		EquipGetOrDrop("Enemy", false);
		AttackZoneColOn(false);
		Audio.instance.sfx[Random.Range(30, 35)].Play();
		yield return new WaitForSeconds(0.02f);
		at.enabled = false;
		IKDynamicOn(true, forceVel);
		StartCoroutine(IE_DeActive());
	}
}
